package com.wuzhi.shop.member.controller;

import java.util.Arrays;
import java.util.Map;

import com.wuzhi.common.exception.BizCodeEnum;
import com.wuzhi.shop.member.exception.PhoneExistException;
import com.wuzhi.shop.member.exception.UserNameExistException;
import com.wuzhi.shop.member.vo.MemberLoginVo;
import com.wuzhi.shop.member.vo.MemberRegisterVo;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.wuzhi.shop.member.entity.MemberEntity;
import com.wuzhi.shop.member.service.MemberService;
import com.wuzhi.common.utils.PageUtils;
import com.wuzhi.common.utils.R;



/**
 * @author gonglize
 */

@RestController
@RequestMapping("member/member")
public class MemberController {
//    写一个测试调用用户的优惠卷。
//    @Autowired
//    CouponFeignService couponFeignService;

    @Autowired
    private MemberService memberService;

//    @RequestMapping("/coupons")
//    public R test(){
//        MemberEntity memberEntity = new MemberEntity();
//        memberEntity.setNickname("快乐");
//
////        调用接口中的方法,得到优惠卷
//        R memberCoupon = couponFeignService.memberCoupon();
//        return R.ok().put("member",memberEntity).put("coupons",memberCoupon.get("coupons"));
//    }

    //注册功能，保存会员userName passWord phone 信息。因为要提交很多数据过来，用post mapping
    @PostMapping("/register")
    public R register(@RequestBody MemberRegisterVo vo) {
        try {
            memberService.register(vo);
        } catch (PhoneExistException e) {
            return R.error(BizCodeEnum.PHONE_EXIST_EXCEPTION.getCode(), BizCodeEnum.PHONE_EXIST_EXCEPTION.getMsg());
        } catch (UserNameExistException e) {
            return R.error(BizCodeEnum.USER_EXIST_EXCEPTION.getCode(), BizCodeEnum.USER_EXIST_EXCEPTION.getMsg());
        }

        return R.ok();
    }

    /**
     * 登录功能:输入账号密码返回数据库的内容
     */
    @PostMapping("/login")
    public R login(@RequestBody MemberLoginVo vo) {
        MemberEntity memberEntity = memberService.login(vo);
        if(memberEntity!=null){
            return R.ok();
        }else {
            return R.error(BizCodeEnum.LOGIN_ACCOUNT_PASSWORD_INVAliD_EXCEPTION.getCode(),BizCodeEnum.LOGIN_ACCOUNT_PASSWORD_INVAliD_EXCEPTION.getMsg());
        }

    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("member:member:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("member:member:info")
    public R info(@PathVariable("id") Long id){
		MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("member:member:save")
    public R save(@RequestBody MemberEntity member){
		memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("member:member:update")
    public R update(@RequestBody MemberEntity member){
		memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("member:member:delete")
    public R delete(@RequestBody Long[] ids){
		memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
